/*
 * @Description: 商品模块控制器
 */
const productDao = require('../models/dao/productDao');

module.exports = {
  /**
   * 获取商品分类
   * @param {Object} ctx
   */
  GetCategory: async ctx => {
    try {
      console.log('GetCategory called');
      const category = await productDao.GetCategory();
      ctx.body = {
        code: '001',
        category
      }
    } catch (error) {
      console.error('GetCategory Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 根据商品分类名称获取首页展示的商品信息
   * @param {Object} ctx
   */
  GetPromoProduct: async ctx => {
    try {
      console.log('GetPromoProduct called with body:', ctx.request.body);
      let { categoryName } = ctx.request.body;
      const categoryID = await productDao.GetCategoryId(categoryName);
      const Product = await productDao.GetPromoProduct(categoryID);
      ctx.body = {
        code: '001',
        Product
      }
    } catch (error) {
      console.error('GetPromoProduct Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 根据商品分类名称获取热门商品信息
   * @param {Object} ctx
   */
  GetHotProduct: async ctx => {
    try {
      console.log('GetHotProduct called with body:', ctx.request.body);
      let { categoryName } = ctx.request.body;
      const categoryID = [];
      for (let i = 0; i < categoryName.length; i++) {
        const category_id = await productDao.GetCategoryId(categoryName[i]);
        categoryID.push(category_id);
      }
      const Product = await productDao.GetProductByCategory(categoryID, 0, 7);
      ctx.body = {
        code: '001',
        Product
      }
    } catch (error) {
      console.error('GetHotProduct Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 分页获取所有的商品信息
   * @param {Object} ctx
   */
  GetAllProduct: async ctx => {
    try {
      console.log('GetAllProduct called with body:', ctx.request.body);
      let { currentPage, pageSize } = ctx.request.body;
      const offset = (currentPage - 1) * pageSize;
      const Product = await productDao.GetAllProduct(offset, pageSize);
      const total = (await productDao.GetAllProduct()).length;
      ctx.body = {
        code: '001',
        Product,
        total
      }
    } catch (error) {
      console.error('GetAllProduct Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 根据分类id,分页获取商品信息
   * @param {Object} ctx
   */
  GetProductByCategory: async ctx => {
    try {
      console.log('GetProductByCategory called with body:', ctx.request.body);
      let { categoryID, currentPage, pageSize } = ctx.request.body;
      const offset = (currentPage - 1) * pageSize;
      const Product = await productDao.GetProductByCategory(categoryID, offset, pageSize);
      const total = (await productDao.GetProductByCategory(categoryID)).length;
      ctx.body = {
        code: '001',
        Product,
        total
      }
    } catch (error) {
      console.error('GetProductByCategory Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 根据搜索条件,分页获取商品信息
   * @param {Object} ctx
   */
  GetProductBySearch: async ctx => {
    try {
      console.log('GetProductBySearch called with body:', ctx.request.body);
      let { search, currentPage, pageSize } = ctx.request.body;
      const offset = (currentPage - 1) * pageSize;
      const category = await productDao.GetCategory();

      let Product;
      let total;

      for (let i = 0; i < category.length; i++) {
        if (search == category[i].category_name) {
          Product = await productDao.GetProductByCategory(category[i].category_id, offset, pageSize);
          total = (await productDao.GetProductByCategory(category[i].category_id)).length;
          ctx.body = {
            code: '001',
            Product,
            total
          }
          return;
        }
      }

      Product = await productDao.GetProductBySearch(search, offset, pageSize);
      total = (await productDao.GetProductBySearch(search)).length;

      ctx.body = {
        code: '001',
        Product,
        total
      }
    } catch (error) {
      console.error('GetProductBySearch Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 根据商品id,获取商品详细信息
   * @param {Object} ctx
   */
  GetDetails: async ctx => {
    try {
      console.log('GetDetails called with body:', ctx.request.body);
      let { productID } = ctx.request.body;
      const Product = await productDao.GetProductById(productID);
      ctx.body = {
        code: '001',
        Product,
      }
    } catch (error) {
      console.error('GetDetails Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  },

  /**
   * 根据商品id,获取商品图片,用于食品详情的页面展示
   * @param {Object} ctx
   */
  GetDetailsPicture: async ctx => {
    try {
      console.log('GetDetailsPicture called with body:', ctx.request.body);
      let { productID } = ctx.request.body;
      const ProductPicture = await productDao.GetDetailsPicture(productID);
      ctx.body = {
        code: '001',
        ProductPicture,
      }
    } catch (error) {
      console.error('GetDetailsPicture Error:', error);
      ctx.body = {
        code: '500',
        msg: '服务器未知错误'
      };
    }
  }
}
